<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Group;
use App\Models\GroupType;
use App\User;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class GroupController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('圈子');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('圈子');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加');
            $content->description('圈子');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Group::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('logo','圈子LOGO')->image('',20,20);
            $grid->column('title','圈子标题');
            $grid->column('post_count','文章数量');

            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Group::class, function (Form $form) {

            $form->select('user_id','用户')->options(User::all()->pluck('name','id'));
            $form->text('title', '圈子标题');
            $form->textarea('detail', '详情');
            $form->radio('type', '类型')->options(['0' => '公共的', '1' => '私有的']);
            $form->select('type_id', '分类')->options(GroupType::selectOptions());
            $form->switch('status', '状态');
            $form->number('sort', '排序')->default(1);
            $form->image('logo', '圈子LOGO');
            $form->image('background', '背景图片');
        });
    }
}
